package com.wcity.manage.service.community.guide.impl;

import com.wcity.manage.common.enums.SaveImageType;
import com.wcity.manage.common.vo.community.guide.GuideVo;
import com.wcity.manage.dao.mapper.community.guide.GuideMapper;
import com.wcity.manage.dao.model.community.guide.Guide;
import com.wcity.manage.dao.model.community.image.ImageUrl;
import com.wcity.manage.service.community.guide.GuideService;
import com.wcity.manage.service.community.image.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 指南类别
 * Created by hgg on 2016/12/20.
 */
@Service
public class GuideServiceImpl implements GuideService {

    @Autowired
    private GuideMapper guideMapper;

    @Autowired
    private ImageService imageService;

    /**
     * 指南类别列表
     * @param queryMap
     * @return
     */
    @Override
    public List<GuideVo> guidePageList( Map<String, Object> queryMap ) {

        return guideMapper.guidePageList( queryMap );
    }

    /**
     * 删除指南类别
     * @param deleteMap
     * @return
     */
    @Override
    public Integer deleteGuide( Map<String, Object> deleteMap ) {

        return guideMapper.deleteGuide( deleteMap );
    }

    /**
     * 新增指南类别
     * @param guide
     * @return
     */
    @Transactional( propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE,rollbackForClassName = "Exception" )
    @Override
    public Integer saveAddGuide( Guide guide,String imgUrl ) {

        //【1】保存指南类别
        Integer saveGuideNum = guideMapper.saveAddGuide( guide );
        if( saveGuideNum == 0 ){
            throw new RuntimeException( "保存指南类别失败" );
        }
        //【2】执行保存相关图片
        ImageUrl imageUrl = new ImageUrl( null,imgUrl,null,guide.getId(),SaveImageType.COMMUNITY_GUIDE );
        Integer saveImgNum = imageService.saveAddImage( imageUrl );
        if( saveImgNum == 0 ){
            throw new RuntimeException( "保存指南类别封面图片失败" );
        }
        return 1;
    }

    /**
     * 更新指南类别
     * @param guide
     * @return
     */
    @Transactional( propagation = Propagation.REQUIRED, isolation = Isolation.SERIALIZABLE,rollbackForClassName = "Exception" )
    @Override
    public Integer saveUpdateGuide( Guide guide,String imgUrl ) {

        //【1】先删除相关封面图片
        Map<String,Object> deleteMap = new HashMap<>();
        deleteMap.put( "referId",guide.getId() );
        deleteMap.put( "saveType", SaveImageType.COMMUNITY_GUIDE );
        Integer deleteNum = imageService.deleteImage( deleteMap );
        if( deleteNum == 0 ){
            throw new RuntimeException( "删除相关封面图片失败" );
        }

        //【2】新增相关封面图片
        ImageUrl imageUrl = new ImageUrl( null,imgUrl,null,guide.getId(),SaveImageType.COMMUNITY_GUIDE );
        Integer saveImgNum = imageService.saveAddImage( imageUrl );
        if( saveImgNum == 0 ){
            throw new RuntimeException( "保存指南类别封面图片失败" );
        }

        //【3】执行更新
        Integer updateNum = guideMapper.saveUpdateGuide( guide );
        if( updateNum == 0 ){
            throw new RuntimeException( "更新指南类别失败" );
        }
        return 1;
    }

    /**
     * 指南类别详情
     * @param detailMap
     * @return
     */
    @Override
    public GuideVo guideDetail( Map<String, Object> detailMap ) {

        return guideMapper.guideDetail( detailMap );
    }

    /**
     * 指南类别总数量
     * @param queryMap
     * @return
     */
    @Override
    public Integer totalGuide( Map<String, Object> queryMap ) {

        return guideMapper.totalGuide( queryMap );
    }

    /**
     * 指南类别列表非分页
     * @param queryMap
     * @return
     */
    @Override
    public List<GuideVo> guideList( Map<String, Object> queryMap ) {

        return guideMapper.guideList( queryMap );
    }

    /**
     * 查询小区ID拼接（1,2,3）
     * @param queryMap
     * @return
     */
    public String blockIds( Map<String,Object> queryMap ){

        return guideMapper.blockIds( queryMap );
    }
}
